package custom.base.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

import static custom.base.utils.ScreenUtil.getVirtualBarHeigh;

/**
 * Created by cubebox on 2017/10/13.
 */

public class KeyBoardUtils {
    public static void showKeyboard(View view) {
        InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, 0);
    }

    public static void hideKeyboard(Activity activity) {
//        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(),
                    0);
        }
    }

    /**
     * @param root         最外层布局，需要调整的布局
     * @param scrollToView 被键盘遮挡的scrollToView，滚动root,使scrollToView在root可视区域的底部
     */
    public static void controlKeyboardLayout(final Activity activity, final View root, final View scrollToView) {
        controlKeyboardLayout(activity, root, scrollToView, null);
    }

    /**
     * @param root         最外层布局，需要调整的布局
     * @param scrollToView 被键盘遮挡的scrollToView，滚动root,使scrollToView在root可视区域的底部
     */
    public static void controlKeyboardLayout(final Activity activity, final View root, final View scrollToView, final OnKeyboardShowListener onKeyboardShowListener) {
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] loc = ScreenUtil.getLocation(scrollToView);
                Rect rect = new Rect();
                //获取root在窗体的可视区域
                root.getWindowVisibleDisplayFrame(rect);
                //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)

                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom - getVirtualBarHeigh(activity);
                //若不可视区域高度大于100，则键盘显示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //获取scrollToView在窗体的坐标
                    scrollToView.getLocationInWindow(location);
                    //计算root滚动高度，使scrollToView在可见区域
                    int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                    if (rootInvisibleHeight > root.getRootView().getHeight() - loc[1] - loc[3]) {
                        root.scrollTo(0, srollHeight);
                    }
                    if (onKeyboardShowListener != null)
                        onKeyboardShowListener.onKeyboardShow();

                } else {
                    //键盘隐藏
                    root.scrollTo(0, 0);
                    if (onKeyboardShowListener != null)
                        onKeyboardShowListener.onKeyboardHide();
                }
            }
        });
    }

    /**
     * 键盘展示监听
     */
    public interface OnKeyboardShowListener {
        void onKeyboardShow();

        void onKeyboardHide();
    }
}
